package com.worthtech.billportal.dao.hibernate;

import java.util.List;

import com.worthtech.billportal.bean.CsAppTerminal;
import com.worthtech.billportal.bean.User;
import com.worthtech.billportal.dao.TerminalDAO;
import com.worthtech.billportal.exception.SCException;
import com.worthtech.billportal.util.AbstractManager;

public class TerminalHibernateDAO extends AbstractManager implements TerminalDAO {
	
	public List getListByHQL(String tableName, int startPage, int perNo,
			String orderby) {
		String hql = "from " + tableName + " " + orderby;
		System.out.println("hql2=" + hql);
		if(perNo==0){
			return this.getHibernateTemplate().find(hql);
		}
		return this.getSession().createQuery(hql).setFirstResult(startPage)
				.setMaxResults(perNo).list();
	}

	public void save(CsAppTerminal terminal) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().save(terminal);
	}
	
	public String getSeqId(String tablename){
		return this.getSession().createSQLQuery("SELECT "+tablename+".NEXTVAL FROM dual").list().get(0).toString();
	}

	public boolean update(CsAppTerminal terminal) {
		try {
			this.getHibernateTemplate().update(terminal);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	public String getCount(String tableName, String condition){
		String hql = "select count(*) from " + tableName + " " + condition;
		System.out.println("hql1=" + hql);
		String tmp = this.getSession().createQuery(hql).list().get(0)
				.toString();
//		System.out.println("count=" + tmp);
		return tmp;
	}
	public void delete(CsAppTerminal c){
		this.getHibernateTemplate().delete(c);
	}

}
